//
//  SThemeUtil.swift
//  niuguwang
//
//  Created by BrightLi on 2021/3/27.
//  Copyright © 2021年 taojinzhe. All rights reserved.
//

import Foundation
import UIKit
import MJRefresh

@objc public enum SAlignType:Int{
    case left = 0
    case center = 1
    case right = 2
}

@objcMembers open class SRefreshHeader: MJRefreshNormalHeader
{
    lazy var descLabel:UILabel={
        let result = SThemeUtil.label("",color:0x8997a5,fontSize:12)
        result.textAlignment = .center
        return result
    }()
}

extension MJRefreshHeader
{
    @objc dynamic public func desc(_ text:String){
        if let order=self as? SRefreshHeader{
            order.descLabel.text=text
            order.layoutSubviews()
        }
    }
}


@objcMembers public class SThemeUtil:NSObject
{
    dynamic public class var emptyCell:UITableViewCell{
        get{
            let empty = UITableViewCell()
            empty.backgroundColor = .white
            return empty
        }
    }
    
    dynamic public class var emptyCollection:UICollectionViewCell{
        get{
            let empty = UICollectionViewCell()
            return empty
        }
    }
    
    dynamic public class func buttonOf(_ icon:String,text:String,color:UIColor,fontSize:CGFloat)->UIButton
    {
        let result=UIButton(type: .custom)
        result.setTitle(text, for: .normal)
        result.setTitleColor(color, for: .normal)
        result.titleLabel?.font=SFitUtil.fitFont(fontSize)
        if let image = SThemeUtil.image(icon){
            result.setImage(image, for: .normal)
            result.setImage(image, for: .highlighted)
        }
        return result
    }
    
    dynamic public class func buttonOf(_ text:String,color:UIColor,pressColor:UIColor,fontSize:CGFloat)->UIButton
    {
        let result=UIButton(type: .custom)
        result.setTitle(text, for: .normal)
        result.setTitleColor(color, for: .normal)
        result.setTitleColor(pressColor, for: .normal)
        result.titleLabel?.font=SFitUtil.fitFont(fontSize)
        return result
    }
    
    dynamic public class func icon(_ name:String) -> UIImageView
    {
        let icon = UIImageView()
        if name.count>0{
            icon.image = UIImage(named:name)
        }
        icon.contentMode = .scaleAspectFill
        icon.clipsToBounds = true
        return icon
    }
    
    dynamic public class func image(_ name:String) -> UIImage?
    {
        if let image = UIImage(named:name){
            return image
        }
        return nil
    }
    
    dynamic public class func label(_ text:String,color:UInt32,fontSize:CGFloat)-> UILabel
    {
        let label = UILabel()
        label.text = text
        label.textColor=SUIUtil.colorOf(color)
        label.font = SFitUtil.fitFont(fontSize)
        label.lineBreakMode = .byTruncatingTail
        return label
    }
    
    dynamic public class func label(_ text:String,color:UInt32,font:String,size:CGFloat) -> UILabel
    {
        let label = UILabel()
        label.text = text
        label.textColor=SUIUtil.colorOf(color)
        label.font = SFitUtil.fitFont(font,size:size)
        return label
    }
    
    dynamic public class func boldLabel(_ text:String,color:UInt32,fontSize:CGFloat) -> UILabel
    {
        let label = UILabel()
        label.text = text
        
        label.textColor=SUIUtil.colorOf(color)
        label.font = SFitUtil.fitBoldFont(fontSize)
        return label
    }
    
    dynamic public class func circle(_ color:UInt32,diameter:CGFloat) -> UIView!
    {
        let circle = UIView(frame:CGRect(x:0,y:0,width:diameter,height:diameter))
        circle.layer.cornerRadius = diameter * 0.5
        circle.layer.backgroundColor = SUIUtil.colorOf(color).cgColor
        circle.layer.masksToBounds = true
        return circle
    }
    
    dynamic public class func viewOf(_ color:UInt32) -> UIView
    {
        return viewOfColor(SUIUtil.colorOf(color))
    }

    dynamic public class func viewOf(_ color:UInt32, alpha:CGFloat = 1) -> UIView
    {
        return viewOfColor(SUIUtil.colorOf(color,alpha:alpha))
    }
    
    dynamic public class func viewOf(_ color:UIColor,borderColor:UIColor) ->UIView{
        let view = UIView()
        view.backgroundColor = color
        view.layer.borderWidth = 0.5
        view.layer.borderColor = borderColor.cgColor
        return view
    }
    
    dynamic public class func hotView(_ debug:Bool=false) ->UIView{
        let view = UIView()
        #if DEBUG
        if debug{
            view.backgroundColor = SUIUtil.colorOf(0xff0000,alpha:0.1)
        }
        #else
        view.backgroundColor = UIColor.clear
        #endif
        return view
    }
    
    dynamic public class func viewOfColor(_ color:UIColor) -> UIView
    {
        let view = UIView()
        view.backgroundColor = color
        return view
    }
    
    dynamic public class func viewOfIcon(_ icon:String) -> UIView
    {
        let view = UIView()
        #if DEBUG
        view.backgroundColor = SUIUtil.colorOf(0xff0000,alpha:0.3)
        #else
        view.backgroundColor = UIColor.clear
        #endif
        let image = SThemeUtil.icon(icon)
        view.addSubview(image)
        image.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        return view
    }
    
    dynamic public class func viewOf(_ color:UInt32,borderColor:UInt32,borderWidth:CGFloat,cornerRadius:CGFloat) -> UIView!
    {
        let view = UIView()
        view.backgroundColor = SUIUtil.colorOf(color)
        view.layer.borderColor = SUIUtil.colorOf(borderColor).cgColor
        view.layer.borderWidth = borderWidth
        view.layer.cornerRadius = cornerRadius
        view.layer.masksToBounds = true
        return view
    }
    dynamic public class func viewOf(_ color:UInt32,alpha:CGFloat ,borderColor:UInt32,borderWidth:CGFloat,cornerRadius:CGFloat) -> UIView!
    {
        let view = UIView()
        view.backgroundColor = SUIUtil.colorOf(color, alpha: alpha)
        view.layer.borderColor = SUIUtil.colorOf(borderColor).cgColor
        view.layer.borderWidth = borderWidth
        view.layer.cornerRadius = cornerRadius
        view.layer.masksToBounds = true
        return view
    }
    
    dynamic public class func buttonOf(_ icon:String) -> UIButton
    {
        let button = UIButton()
        button.setImage(UIImage.init(named: icon), for: .normal)
        button.setImage(UIImage.init(named: icon), for: .highlighted)
        return button
    }
    
    dynamic public class func tableView(_ style:UITableView.Style) -> UITableView
    {
        let tableView = UITableView(frame: CGRect.zero, style: style)
        tableView.estimatedRowHeight=0
        tableView.estimatedSectionHeaderHeight=0
        tableView.estimatedSectionFooterHeight=0
        tableView.separatorColor=UIColor.clear
        tableView.separatorStyle = .none
        tableView.backgroundColor = UIColor.white
        if #available(iOS 11.0, *){
            tableView.contentInsetAdjustmentBehavior = .never
        }
        return tableView
    }
    dynamic public class func tableView() -> UITableView
    {
        let tableView = UITableView()
        tableView.estimatedRowHeight=0
        tableView.estimatedSectionHeaderHeight=0
        tableView.estimatedSectionFooterHeight=0
        tableView.separatorStyle = .none
        tableView.separatorColor=UIColor.clear
        tableView.backgroundColor = UIColor.white
        if #available(iOS 11.0, *){
            tableView.contentInsetAdjustmentBehavior = .never
        }
        return tableView
    }
    
    dynamic public class func regCell(_
                                      tableView:UITableView!,cls:AnyClass)
    {
        let identifier = NSStringFromClass(cls)
        tableView.register(cls, forCellReuseIdentifier: identifier)
    }
    
    dynamic public class func footer(_ tableView:UITableView,color:UIColor,height:CGFloat)
    {
        let footer:UIView = UIView()
        footer.backgroundColor = color
        footer.frame = CGRect(x: 0, y: 0, width: CGFloat(ScreenWidth), height:height)
        tableView.tableFooterView = footer
    }
    
    dynamic public class func footer(_ tableView:UITableView!)
    {
        let footer:UIView = UIView()
        footer.backgroundColor = UIColor.clear
        footer.frame = CGRect(x: 0, y: 0, width: CGFloat(ScreenWidth), height: CGFloat(SFitUtil.fit(80)))
        tableView.tableFooterView = footer
    }
    
    dynamic public class func cellOf(_ tableView:UITableView!,cls:AnyClass) -> UITableViewCell!
    {
        if let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(cls)){
            return cell
        }
        return UITableViewCell()
    }

    dynamic public class func refershHead(_ target:UIScrollView,block:@escaping ChangeBlock){
        let header = SRefreshHeader.init {
            block()
        }
        // 设置箭头
        header.arrowView?.image = UIImage(named: "tools_refresh_arrow")
        // 设置文字
        header.setTitle("下拉可以刷新", for: .idle)
        header.setTitle("松开后刷新", for: .pulling)
        header.setTitle("正在加载...", for: .refreshing)
        // 设置字体
        header.stateLabel?.font = SFitUtil.fitFont(14)
        header.lastUpdatedTimeLabel?.font = SFitUtil.fitFont(12)
        // 设置颜色
        header.stateLabel?.textColor = SUIUtil.colorOf(0x8997a5)
        header.lastUpdatedTimeLabel?.textColor = SUIUtil.colorOf(0x8997a5)
        header.loadingView?.style = .gray
        target.mj_header = header
        // 设置自动切换透明度(在导航栏下面自动隐藏)
        target.mj_header?.isAutomaticallyChangeAlpha = true
    }
    
    dynamic public class func refershFooter(_ target:UIScrollView,block:@escaping ChangeBlock){
        let footer = MJRefreshBackNormalFooter.init {
            block()
        }
        // 设置箭头
        footer.arrowView?.image = UIImage(named: "tools_refresh_arrow")
        // 设置文字
        footer.setTitle("上拉可以刷新", for: .idle)
        footer.setTitle("松开后刷新", for: .pulling)
        footer.setTitle("我是有底线的", for: .noMoreData)
        // 设置字体
        footer.stateLabel?.font = SFitUtil.fitFont(14)
        // 设置颜色
        footer.stateLabel?.textColor = SUIUtil.colorOf(0x8997a5)
        footer.loadingView?.style = .gray
        target.mj_footer = footer
        // 设置自动切换透明度(在导航栏下面自动隐藏)
        target.mj_footer?.isAutomaticallyChangeAlpha = true
    }
    dynamic public class func refershWhiteThemeHead(_ target:UIScrollView,block:@escaping ChangeBlock){
        let header = SRefreshHeader.init {
            block()
        }
        // 设置箭头
        header.arrowView?.image = UIImage(named: "tools_refresh_arrow_white")
        // 设置文字
        header.setTitle("下拉可以刷新", for: .idle)
        
        header.setTitle("松开后刷新", for: .pulling)
        header.setTitle("正在加载...", for: .refreshing)
        // 设置字体
        header.stateLabel?.font = SFitUtil.fitFont(14)
        header.lastUpdatedTimeLabel?.font = SFitUtil.fitFont(12)
        // 设置颜色
        header.stateLabel?.textColor = SUIUtil.colorOf(0xffffff)
        header.lastUpdatedTimeLabel?.textColor = SUIUtil.colorOf(0xffffff)
        header.loadingView?.style = .white
        target.mj_header = header
        // 设置自动切换透明度(在导航栏下面自动隐藏)
        target.mj_header?.isAutomaticallyChangeAlpha = true
    }
    
    
    // MARK: - 当前window
    class func keyWindow() -> UIWindow {
         if #available(iOS 15.0, *) {
             let keyWindow = UIApplication.shared.connectedScenes
                 .map({ $0 as? UIWindowScene })
                 .compactMap({ $0 })
                 .first?.windows.first ?? UIWindow()
             return keyWindow
         }else {
             let keyWindow = UIApplication.shared.windows.first ?? UIWindow()
             return keyWindow
         }
     }

    
}
